﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Business.Menu;
using System.Text;

public partial class SystemManager_MenuManage : PageBase
{
    /// <summary>
    /// 菜单及菜单组
    /// 修改人：YZG
    /// 时间：2010-03-01
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var Menu = new XtMenuOperation();

            /***************删除子菜单*****************/
            if (!string.IsNullOrEmpty(Request["delmenuchildid"]))
            {
                Menu.DeleteChildMenu(Request["delmenuchildid"]);
                //msgAppear.Visible = true;
                msgAppear.Text = "子菜单删除成功";
            }

            /***************删除菜单组****************/
            if (!string.IsNullOrEmpty(Request["delmenuparentid"]))
            {
                Menu.DeleteMenuGroup(Request["delmenuparentid"]);
                msgAppear.Text = "菜单组删除成功";
            }

            /***************删除一级菜单****************/
            if (!string.IsNullOrEmpty(Request["delfirstmenuid"]))
            {
                Menu.DeleteFirstMenu(Request["delfirstmenuid"]);
                msgAppear.Text = "一级菜单删除成功";
            }

            /***************子菜单上移****************/
            if (!string.IsNullOrEmpty(Request["ChildMupId"]) && Convert.ToInt32(Request["ChildMno"]) >= 1)
            {
                Menu.MoveMenu(Convert.ToInt32(Request["ChildMno"]), Request["ChildMupParId"], Request["ChildMupId"]);
            }

            /**************子菜单下移*****************/
            if (!string.IsNullOrEmpty(Request["ChildMdownId"]))
            {
                Menu.MoveDownMenu(Convert.ToInt32(Request["ChildMdownno"]), Request["ChildMdownParId"], Request["ChildMdownId"]);
            }

            /***************菜单组上移******************/
            if (!string.IsNullOrEmpty(Request["ParUpId"]) && Convert.ToInt32(Request["ParUpNo"]) >= 1)
            {
                Menu.MoveUpMenuGroup(Convert.ToInt32(Request["ParUpNo"]), Request["ParUpId"]);
            }
            /***************菜单组下移******************/
            if (!string.IsNullOrEmpty(Request["ParDownId"]))
            {
                Menu.MoveDownMenuGroup(Convert.ToInt32(Request["ParDownNo"]), Request["ParDownId"]);
            }


            /***************一级菜单上移******************/
            if (!string.IsNullOrEmpty(Request["FirstUpId"]) && Convert.ToInt32(Request["FirstUpNo"]) >= 1)
            {
                Menu.MoveUpMenuFirst(Convert.ToInt32(Request["FirstUpNo"]), Request["FirstUpId"]);
            }

            /***************一级菜单下移******************/
            if (!string.IsNullOrEmpty(Request["FirstDownId"]))
            {
                Menu.MoveDownMenuFirst(Convert.ToInt32(Request["FirstDownNo"]), Request["FirstDownId"]);
            }
            var html = new StringBuilder();
            html.Append("<table class=\"tab\" width=\"100%\" border=\"1\"><tr class=\"title\" align=\"center\" valign=\"middle\">");
            html.Append("<th width=\"8%\">一级菜单</th><th width=\"8%\">菜单组名称</th><th width=\"13%\">子菜单名称</th><th width=\"50%\">链接页面</th><th width=\"25%\">操作</th></tr>");
            var dtFirstMenu = Menu.GenerateFirstMenu();
            var i = 0;
            dtFirstMenu.Rows.Cast<DataRow>().ToList().ForEach(row =>
            {
                html.Append("<tr>");
                html.Append(string.Format("<td>{0}</td><td></td><td></td><td></td>", row["menuname"].ToString()));
                html.Append(string.Format("<td><a href=\"MenuManage.aspx?FirstUpId={0}&FirstUpNo={1}\">上移</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", row["firstmenuid"], row["show_no"]));
                html.Append(string.Format("<a href=\"MenuManage.aspx?FirstDownId={0}&FirstDownNo={1}\">下移</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", row["firstmenuid"], row["show_no"]));
                html.Append(string.Format("<a href=\"UpdateFirstMenu.aspx?parentMname={0}&parentMpic={1}&parentMid={2}&showno={3}\">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", row["menuname"], row["icon_url"], row["firstmenuid"], row["show_no"]));
                html.Append(string.Format("<a href=\"MenuManage.aspx?delfirstmenuid={0}\" onclick=\"return confirm('确定删除？')\">删除</a></td>", row["firstmenuid"]));
                html.Append("</tr>");
                var dtParent = Menu.GenerateParentMenuById(row["firstmenuid"].ToString());
                dtParent.Rows.Cast<DataRow>().ToList().ForEach(parent =>
                {
                    html.Append("<tr>");
                    html.Append(string.Format("<td></td><td>{0}</td><td></td><td></td>", parent["menu_parent_desc"]));
                    html.Append(string.Format("<td><a href=\"MenuManage.aspx?ParUpId={0}&ParUpNo={1}\">上移</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", parent["menu_parent_id"], parent["show_no"]));
                    html.Append(string.Format("<a href=\"MenuManage.aspx?ParDownId={0}&ParDownNo={1}\">下移</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", parent["menu_parent_id"], parent["show_no"]));
                    html.Append(string.Format("<a href=\"UpdateGroupMenu.aspx?parentMname={0}&parentMpic={1}&parentMid={2}&showno={3}&firstMenuId={4}\">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", parent["menu_parent_desc"], parent["picpath"], parent["menu_parent_id"], parent["show_no"], row["firstmenuid"]));
                    html.Append(string.Format("<a href=\"MenuManage.aspx?delmenuparentid={0}\" onclick=\"return confirm('确定删除？')\">删除</a></td>", parent["menu_parent_id"]));
                    html.Append("</tr>");
                    var dtchild = Menu.GetChildMenu(parent["menu_parent_id"].ToString());
                    dtchild.Rows.Cast<DataRow>().ToList().ForEach(child =>
                    {
                        html.Append("<tr><td></td><td></td>");
                        html.Append(string.Format("<td>{0}</td>", child["menu_child_desc"]));
                        html.Append(string.Format("<td>{0}</td>", child["linkhref"]));
                        html.Append(string.Format("<td><a href=\"MenuManage.aspx?ChildMupId={0}&ChildMno={1}&ChildMupParId={2}\">上移</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", child["menu_child_id"], child["show_no"], child["ancestor"]));
                        html.Append(string.Format("<a href=\"MenuManage.aspx?ChildMdownId={0}&ChildMdownno={1}&ChildMdownParId={2}\">下移</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", child["menu_child_id"], child["show_no"], child["ancestor"]));
                        html.Append(string.Format("<a href=\"UpdateChildMenu.aspx?childMname={0}&childMpic={1}&childMurl={2}&childMpar={3}&childMid={4}&showno={5}\">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", child["menu_child_desc"], child["overpic"], System.Web.HttpUtility.UrlEncode(child["linkhref"].ToString()), child["ancestor"], child["menu_child_id"], child["show_no"]));
                        html.Append(string.Format("<a href=\"MenuManage.aspx?delmenuchildid={0}\" onclick=\"return confirm('确定删除？')\">删除</a></td>", child["menu_child_id"]));
                        html.Append("</tr>");
                    });
                });
            });
            html.Append("</table>");
            context.InnerHtml = html.ToString();
        }
    }
}
